package cn.wizzer.app.web.modules.controllers.platform.sys;


import cn.wizzer.app.sys.modules.models.SysIpWhiteList;
import cn.wizzer.app.sys.modules.services.SysIpWhiteListService;
import cn.wizzer.framework.base.model.enums.BizExceptionEnum;
import cn.wizzer.framework.util.Result;
import com.alibaba.dubbo.config.annotation.Reference;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.nutz.dao.Chain;
import org.nutz.dao.Cnd;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.lang.Strings;
import org.nutz.lang.Times;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.Param;

import javax.servlet.http.HttpServletRequest;

@IocBean
@At("/platform/sys/ip/whiteList")
public class SysIpWhiteListController {
    @Inject
    @Reference
    private SysIpWhiteListService ipWhiteListService;

    @At("")
    @Ok("beetl:/platform/sys/ip/whiteList/index.html")
    @RequiresPermissions("sys.manager.ip.whiteList")
    public void index(HttpServletRequest req) {

    }


    /**
     * 白名单列表
     *
     * @param searchKeyword
     * @param searchDate
     * @param pageNumber
     * @param pageSize
     * @return
     */
    @At
    @Ok("json:full")
    @RequiresPermissions("sys.manager.ip.whiteList")
    public Object data(@Param("searchKeyword") String searchKeyword,
                       @Param("searchDate") String[] searchDate,
                       @Param("pageNumber") int pageNumber,
                       @Param("pageSize") int pageSize) {
        Cnd cnd = Cnd.NEW();

        if (Strings.isNotBlank(searchKeyword)) {
            cnd.and("ip", "like", "%" + searchKeyword + "%");
        }
        if (searchDate != null && searchDate.length > 0) {
            cnd.and("ctAt", ">=", Times.d2TS(Times.D(searchDate[0])))
                    .and("ctAt", "<=", Times.d2TS(Times.D(searchDate[1])));
        }
        cnd.orderBy("ctAt", "desc");
        return Result.success().addData(ipWhiteListService.listPage(pageNumber, pageSize, cnd));
    }


    /**
     * 添加白名单
     *
     * @param ipWhiteList
     * @return
     */
    @At
    @Ok("json:full")
    @RequiresPermissions("sys.manager.ip.whiteList.add")
    public Object add(@Param("..") SysIpWhiteList ipWhiteList) {
        if (null != ipWhiteListService.fetch(Cnd.where("ip", "=", ipWhiteList.getIp()))) {
            return Result.error(BizExceptionEnum.IP_EXISTS);
        }
        ipWhiteListService.add(ipWhiteList);
        return Result.success();
    }

    /**
     * 更新白名单（包括启用禁用）
     *
     * @param ipWhiteList
     * @return
     */
    @At
    @Ok("json:full")
    @RequiresPermissions("sys.manager.ip.whiteList.edit")
    public Object update(@Param("..") SysIpWhiteList ipWhiteList) {
        ipWhiteListService.update(ipWhiteList);
        return Result.success();
    }

    @At
    @Ok("json:full")
    @RequiresPermissions("sys.manager.ip.whiteList.delete")
    public Object delete(@Param("id") String id) {
        ipWhiteListService.del(id);
        return Result.success();
    }

}
